package com.tqyan.service.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tqyan.dal.mapper.ChatMapper;
import com.tqyan.dal.mapper.UserSettingMapper;
import com.tqyan.model.controller.response.ChatContentResp;
import com.tqyan.model.dto.ChatContentDTO;
import com.tqyan.model.dto.ChatDTO;
import com.tqyan.model.dto.UserDTO;
import com.tqyan.model.dto.UserSettingDTO;
import com.tqyan.model.enums.ChatModelEnum;
import com.tqyan.model.enums.ChatTypeEnum;
import com.tqyan.model.exp.DefaultException;
import com.tqyan.service.chat.ChatPointContext;
import com.tqyan.service.service.AccountService;
import com.tqyan.service.service.ChatChatService;
import com.tqyan.service.service.ChatService;
import com.tqyan.service.service.UserService;
import com.tqyan.service.transfer.LimitService;
import com.tqyan.util.DisableWordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会话-聊天服务
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/4/12
 */
@Service
@Slf4j
public class ChatChatServiceImpl implements ChatChatService {

    @Autowired
    private UserSettingMapper userSettingMapper;
    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private UserService userService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ChatService chatService;
    @Autowired
    private LimitService limitService;
    @Autowired
    private ChatPointContext chatPointContext;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChatContentResp chat(String openId, Long chatId, String content) {
        if (StringUtils.isBlank(content)) {
            throw new DefaultException("内容可不为空");
        }
        // 问题最多200个字符
        if (content.length() > 200) {
            throw new DefaultException("问题最多200个字符");
        }
        // 禁用词检查
        if (DisableWordUtil.containsDisable(content)) {
            throw new DefaultException("请文明用语");
        }
        // 获取用户信息
        UserDTO user = userService.getUserInfo(openId);
        // 获取用户设置, TODO 后期还原回会话设置
        UserSettingDTO userSetting = userSettingMapper.queryByUser(user.getId());
        ChatModelEnum chatModel = ChatModelEnum.get(userSetting.getModel());
        if (null == chatModel) {
            throw new DefaultException("会话模式错误");
        }
        // 是否欠费
        if (accountService.isArrears(user.getId(), chatModel.getPrice())) {
            throw new DefaultException("501", "余额不足");
        }
        // 普通模式, User限流
//        if (ChatModelEnum.NORMAL.equals(chatModel)) {
//            if (limitService.checkLimit(openId)) {
//                throw new DefaultException("让小燕子休息一会吧");
//            }
//        }
        // 获取会话信息
        ChatDTO chat = null;
        if (null != chatId) {
            chat = chatMapper.queryByChat(user.getId(), chatId);
        }
        if (null == chat) {
            chat = chatService.createChat(user, content, ChatTypeEnum.CHAT);
        }
        // 根据指令, 获取回答
        ChatContentDTO answer = chatPointContext.chat(user, userSetting, chat, content);
        // 响应
        ChatContentResp resp = new ChatContentResp();
        resp.setUserId("0");
        resp.setChatId(answer.getChatId() + "");
        resp.setChatContentId(answer.getId() + "");
        resp.setContent(answer.getContent());
        resp.setType(answer.getType());
        resp.setStatus(answer.getStatus());
        // convert
        chatService.handleContent(resp);
        return resp;
    }

}


